import torch

# 创建两个大小为 (2, 3) 的张量
tensor1 = torch.tensor([[1, 2, 3], [4, 5, 6]])
tensor2 = torch.tensor([[7, 8, 9], [10, 11, 12]])
tensor3 = torch.tensor([[-1, -2, -3], [-4, -5, -6]])

# 沿着第一个维度连接这两个张量
result = torch.cat((tensor1, tensor2), dim=0)
print(result)
# tensor([[ 1,  2,  3],
#         [ 4,  5,  6],
#         [ 7,  8,  9],
#         [10, 11, 12]])

result1 = torch.cat(([tensor1, tensor2, tensor3]), dim=1)
print(result1)
# tensor([[ 1,  2,  3,  7,  8,  9, -1, -2, -3],
#         [ 4,  5,  6, 10, 11, 12, -4, -5, -6]])

# 不指定dim，默认为0
result2 = torch.cat((tensor1, tensor2))
print(result2)
# tensor([[ 1,  2,  3],
#         [ 4,  5,  6],
#         [ 7,  8,  9],
#         [10, 11, 12]])